//
//  ViewController.m
//  序列帧动画
//
//  Created by 曹亮 on 16/11/21.
//  Copyright © 2016年 曹亮. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic,weak)  UIImageView *imageView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];


    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    imageView.center = self.view.center;
    [self.view addSubview:imageView];

    self.imageView = imageView;


    //开始动画
    [self startAnimation];

}

-(void)startAnimation{

    //加载动画数组
    NSArray *arr = [self loadAnimationImages];

    //设置动画数组
    [self.imageView setAnimationImages:arr];

    //设置每张图片显示间隔
    [self.imageView setAnimationDuration:arr.count * 0.075];

    //设置动画次数
    [self.imageView setAnimationRepeatCount:1];

    //开启动画
    [self.imageView startAnimating];

    //动画结束后清除缓存
    [self performSelector:@selector(clearAinimationImageMemory) withObject:nil afterDelay:self.imageView.animationDuration];

}

// 加载数组
-(NSArray *)loadAnimationImages{

    NSMutableArray *arrM = [NSMutableArray array];

    for (NSInteger i = 1; i < 105; i++) {
        //性能优化
        NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"00%zd",i] ofType:@"png"];

        UIImage *image = [UIImage imageWithContentsOfFile:path];

        [arrM addObject:image];

    }

    return arrM.copy;
}


// 清除animationImages所占用内存
- (void)clearAinimationImageMemory {
    
    [_imageView stopAnimating];
    
    _imageView.animationImages = nil;
    
}

@end
